/*
 * @Author: whr2349
 * @Date: 2021-07-05 09:54:31
 * @LastEditors: whr2349
 * @LastEditTime: 2021-07-08 15:54:43
 * @Description: file content
 * @FilePath: \back\app\service\user.js
 */
'use strict';

const Service = require('egg').Service;

class UserService extends Service {
    /**
     * 返回用户列表
     * @param { number } currentPage 
     * @param { number } pageSize 
     * @param { object } sort 
     * @returns 
     */
    async index(currentPage, pageSize, sort = { createdTime: -1 }) {
        const { ctx } = this
        const result = await ctx.model.User.aggregate([
            {
                $lookup: {
                    from: "organization",
                    localField: "organization",
                    foreignField: "_id",
                    as: "organization",
                },
            },
            {
                $unwind: '$organization'
            },
            {
                $skip: (currentPage - 1) * pageSize
            },
            {
                $limit: parseInt(pageSize)
            },
            {
                $sort: sort
            }

        ])
        return result
    }
    async create(data) {
        const { ctx } = this
        const result = await ctx.model.User.create(data)
        return result
    }
    async update(data) {
        const { ctx } = this
        const result = await ctx.model.User.updateOne(data)
        return result
    }
}

module.exports = UserService;
